<?php

namespace App\Services\User;

use App\Models\User\UserClient;
use App\Models\User\UsersInfo;
use App\Services\User\User\UserLogic;
use Illuminate\Support\Facades\DB;

/**
 * Class User 用户服务
 * @package App\Services\User
 */
class User
{

    // 用户登录
    public function login($data)
    {
        $data = pick_array($data, ['id', 'name', 'phone', 'login_password', 'token', 'is_auto_login']);
        ValidateUser::checkValidate($data, ['name', 'phone', 'login_password', 'token', 'is_auto_login']);

        return (new UserLogic())->login($data);
    }
//
//    //用户自动登录
//    public function autoLogin(): array
//    {
//        $data = $this->haveIssetReceiveData(['name'=>'', 'phone'=>'', 'token' => ' ', 'auto_login_time'=>0, 'auto_login_token'=>'']);
//        if ($data['name'] === '' && $data['phone'] === '') {
//            return verify_err(null, '用户名和手机号至少要输入一个');
//        }
//        return $this->stepRun($data, 'autoLogin', 'autoLogin');
//    }
//
//    /*自动登录超时后的处理*/
//    public function autoLoginOvertime(): array
//    {
//        $data = $this->haveIssetReceiveData(['name'=>' ']);
//        return $this->stepRun($data, 'autoLoginOvertime');
//    }
//
//     用户注册
    public function register($data): array
    {
        $data = pick_array($data, ['phone', 'code', 'login_password', 'type', 'name', 'other_info', 'token']);
        ValidateUser::checkValidate($data, ['phone', 'code', 'login_password', 'type', 'name', 'other_info', 'token']);
        $data['other_info'] = '{"authen_status": -1}';
        $data['enterprise_info'] = '{"authen_status": -1}';
        return (new UserLogic())->register($data);
    }
//    // 用户完善信息
//    public function perfect(): array
//    {
//        $data = $this->haveIssetReceiveData(['name'=>'', 'id'=>'', 'phone'=>'', 'other_info' => '{}']);
//        if(array_eq($data, '', ['name','id','phone'])){
//            return verify_err($data, 'name,id,phone至少要传一个');
//        } else if ($data['other_info'] === '{}'){
//            return verify_err($data, 'other_info不能为空');
//        } else if (is_array($data['other_info'])){
//            $data['other_info'] = json_encode($data['other_info']);
//        }
//        return $this->stepRun($data, 'perfect', 'perfect');
//    }

    // 用户审核通过
    public function passAudit($data): array
    {
        $map = make_map($data, ['id', 'name', 'phone'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->passAudit($data);
    }

    // 用户分页列表数据
    public function list($data): array
    {
        //ValidateUser::checkValidate($data, ['page', 'list_rows']);
        return (new UserLogic())->list($data);
    }

    // 更改用户信息(后台使用)
    public function edit($data): array
    {
        $map = make_map($data, ['id', 'name', 'nickname', 'sex', 'phone', 'row_ids', 'row_group_ids', 'other_info'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->edit($data);
    }
//    // 验证登录密码
//    public function checkLoginPassword(): array
//    {
//        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' ']);
//        if(array_eq($data, '', ['id', 'name', 'phone'])){
//            return verify_err(null, 'id,name,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'checkLoginPassword', 'checkLoginPassword');
//    }
//    // 验证支付密码
//    public function checkPayPassword(): array
//    {
//        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' ']);
//        if(array_eq($data, '', ['id', 'name', 'phone'])){
//            return verify_err(null, 'id,name,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'checkPayPassword', 'checkPayPassword');
//    }
//    // 修改登录密码
    public function editLoginPassword(): array
    {
        $setArr = ['id' => '', 'name' => '', 'phone' => '', 'old_login_password' => ' ', 'login_password' => ' ', 're_login_password' => ' '];
        $data = $this->haveIssetReceiveData($setArr);
        if (array_eq($data, '', ['id', 'name', 'phone'])) {
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'editLoginPassword', 'editLoginPassword');
    }
//    // 修改支付密码
//    public function editPayPassword(): array
//    {
//        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'old_pay_password'=>' ', 'pay_password'=>' ', 're_pay_password'=>' '];
//        $data = $this->haveIssetReceiveData($setArr);
//        if(array_eq($data, '', ['id', 'name', 'phone'])){
//            return verify_err(null, 'id,name,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'editPayPassword', 'editPayPassword');
//    }
//    // 忘记登录密码-用手机验证码  # TODO 废弃
//    public function forgetLoginPassword(): array
//    {
//        $setArr = ['name'=>' ', 'phone'=>' ', 'phone_code'=>' ', 'login_password'=>' ', 're_login_password'=>' '];
//        $data = $this->haveIssetReceiveData($setArr);
//        return $this->stepRun($data, 'forgetLoginPassword', 'forgetLoginPassword');
//    }
//    // 忘记支付密码 # TODO 废弃
//    public function forgetPayPassword(): array
//    {
//        return auto_success(123);
//    }
//    // 重置用户登录密码-中间层验证手机验证码通过后再调用本方法
//    public function resetLoginPassword(): array
//    {
//        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' '];
//        $data = $this->haveIssetReceiveData($setArr);
//        if(array_eq($data, '', ['id', 'name', 'phone'])){
//            return verify_err(null, 'id,name,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'resetLoginPassword', 'resetLoginPassword');
//    }
//    // 重置用户支付密码-中间层验证手机验证码通过后再调用本方法
//    public function resetPayPassword(): array
//    {
//        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' '];
//        $data = $this->haveIssetReceiveData($setArr);
//        if(array_eq($data, '', ['id', 'name', 'phone'])){
//            return verify_err(null, 'id,name,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'resetPayPassword', 'resetPayPassword');
//    }
//    // 根据tocken获取某一用户信息
//    public function infoByTocken(): array
//    {
//        return auto_success(123);
//    }
    /**
     * 根据 ID ||用户名 || 手机号 获取某一用户信息
     *
     * @param array $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function info($data = []): array
    {
        $map = make_map($data, ['id', 'phone'], '');
        # 显式数据验证
        ValidateUser::checkValidate($data, array_keys($map));
        # 底层封装
        return (new UserLogic())->info($data);
    }

    // 根据 ID ||用户名 || 手机号 获取某一用户包含自动登录的用户全部信息
    public function infoAll($data = []): array
    {
        $data = init_array($data, ['id' => 0, 'name' => '', 'phone' => '']);
        $map = make_map($data, ['id', 'name', 'phone'], '');
        # 显式数据验证
        if (array_eq($data, '', ['name', 'id', 'phone'])) {
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->infoAll($data);
    }
//    // 初始化用户支付密码 在用户没有设置支付密码的情况下
//    public function initPayPassword(): array
//    {
//        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'login_password' => ' ', 'pay_password' => ' ']);
//        if(array_eq($data, '', ['name','id','phone'])){
//            return verify_err($data, 'name,id,phone至少要传一个');
//        }
//        return $this->stepRun($data, 'initPayPassword', 'initPayPassword');
//    }
    // 根据用户传入的IDS获取用户信息
    public function infoByIds($data): array
    {
        if ('' === $data['ids']) {
            return verify_err('请传入参数ids');
        }
        ValidateUser::checkValidate($data, ['ids']);
        return (new UserLogic())->infoByIds($data);
    }

    // 用户退出登录
    public function logout($data): array
    {
        ValidateUser::checkValidate($data, ['token']);
        return (new UserLogic())->logout($data);
    }

    // 给某一个用户某角色
    public function giveRoles($data): array
    {
        $data = init_array($data, ['id' => '', 'name' => '', 'phone' => '', 'role_id' => '0']);

        return (new UserLogic())->giveRoles($data);
    }

    // 移除某用户的某角色
    public function removeUserRole($data): array
    {
        $data = init_array($data, ['id' => '', 'name' => '', 'phone' => '', 'role_id' => '0']);

        return (new UserLogic())->removeUserRole($data);
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'role_id' => '0']);

        return $this->stepRun($data, 'removeUserRole', 'removeUserRole');
    }

    // 用户更改自己的信息
    public function editSelfInfo($data): array
    {
        ValidateUser::checkValidate($data, ['nickname']);
        return (new UserLogic())->editSelfInfo($data);
    }

    // 用户更改自己的登录密码
    public function editSelfLoginPassword($data): array
    {
        ValidateUser::checkValidate($data, ['token', 'login_password', 'old_login_password', 're_login_password']);
        return (new UserLogic())->editSelfLoginPassword($data);
    }

    // 管理员重置普通用户登录密码
    public function resetUserLoginPassword($data): array
    {
        $map = make_map($data, ['id', 'name', 'phone', 'login_password', 're_login_password'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->resetUserLoginPassword($data);
    }

    // 超级管理员重置所有用户密码的接口
    public function resetAllLoginPassword($data): array
    {
        $map = make_map($data, ['id', 'name', 'phone', 'login_password', 're_login_password'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->resetAllLoginPassword($data);
    }

    // 用户更改自己的支付密码
    public function editSelfPayPassword($data): array
    {
        ValidateUser::checkValidate($data, ['token', 'pay_password', 'old_pay_password', 're_pay_password']);
        return (new UserLogic())->editSelfPayPassword($data);
    }

    // 管理员重置普通用户支付密码
    public function resetUserPayPassword($data): array
    {
        $map = make_map($data, ['id', 'name', 'phone', 'pay_password', 're_pay_password'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->resetUserPayPassword($data);
    }

    // 管理员重置所有用户支付密码
    public function resetAllPayPassword($data): array
    {
        $map = make_map($data, ['id', 'name', 'phone', 'pay_password', 're_pay_password'], '');
        ValidateUser::checkValidate($data, array_keys($map));
        return (new UserLogic())->resetAllPayPassword($data);
    }
//
//    //用户修改个人基本信息 包括other_info
//    public function updateUserInfo(): array
//    {
//        $data = $this->haveIssetReceiveData(['token' => ' ', 'nickname' => '', 'sex' => '', 'head_portrait'=>'', 'other_info'=>'{}']);
//        if($data['other_info'] === '{}' && array_eq($data, '', ['nickname', 'sex', 'head_portrait'])){
//            return verify_err($data, 'nickname,sex,other_info,head_portrait至少要传一个');
//        }
//        if ($data['other_info'] !== '{}' && is_array($data['other_info'])){
//            $data['other_info'] = json_encode($data['other_info']);
//        }
//        return $this->stepRun($data, 'updateUserInfo', 'updateUserInfo');
//    }
//
//    //用户进行实名认证
//    public function authentication(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'id_number'=>' ', 'front_img'=>' ', 'back_img'=>' ','real_name'=>' ']);
//        return $this->stepRun($data, 'authentication', 'authentication');
//    }
//
//    /*用户进行换绑手机*/
//    public function changePhone(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ','phone'=>' ']);
//        return $this->stepRun($data, 'changePhone', 'changePhone');
//    }

    /*管理员审核用户的实名认证信息*/
    public function verifyAuthentication($data): array
    {
        ValidateUser::checkValidate($data, ['id', 'status', "type"]);
        return (new UserLogic())->verifyAuthentication($data);
    }

    /*管理员获取用户的实名认证列表（个人信息）*/
    public function getAuthticationList($data): array
    {
        ValidateUser::checkValidate($data, ['status', 'keywords']);
        return (new UserLogic())->getAuthticationList($data);
    }

    /*管理员获取用户的实名认证列表（企业信息）*/
    public function getEnterpriseInfo($data): array
    {
        ValidateUser::checkValidate($data, ['status', 'keywords']);
        return (new UserLogic())->getEnterpriseInfo($data);
    }
//
//    /*获取我的账户安全级别*/
//    public function getSecurityLevel(): array
//    {
//        $data = $this->haveIssetReceiveData(['user_id'=>0]);
//        return $this->stepRun($data, 'getSecurityLevel');
//    }
//
    /**
     * 最近一周每天的用户注册量(新增用户) / 平台总用户
     * SELECT COUNT(*) AS counts,FROM_UNIXTIME(create_time,'%Y-%m-%d') AS create_time_date FROM tp_user WHERE create_time>UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 WEEK)) GROUP BY create_time_date    其中WEEK可换成DAY MONTH YEAR
     * @param $data
     * @return array
     */
    public function userIncreTendency($data): array
    {
        $data = init_array($data, ['dbDateFormat' => '%y-%m-%d', 'dateFormat' => 'y-m-d']);
        return (new UserLogic())->userIncreTendency($data);
    }

    // 后台添加管理员
    public function addManager($data): array
    {
        ValidateUser::checkValidate($data, ['name', 'real_name', 'phone', 'email', 'role_ids']);
        return (new UserLogic())->addManager($data);
    }


    // 后台添加客户端用户信息
    public function addClientUser($data): array
    {
        ValidateUser::checkValidate($data, ['phone', 'card']);
        return (new UserLogic())->addClientUser($data);
    }

    //根据id查询客户详情
    public function userClientDetail($data)
    {
        // ValidateUser::checkValidate($data, ['name', 'nickname', 'phone', 'login_password', 'other_info']);
        return (new UserLogic())->userClientDetail($data);
    }

    //根据id查询客户详情
    public function userEditDetail($data)
    {
        // ValidateUser::checkValidate($data, ['name', 'nickname', 'phone', 'login_password', 'other_info']);
        return (new UserLogic())->userEditDetail($data);
    }

    //领取任务
    public function userGetTask()
    {
        //ValidateUser::checkValidate($data, ['name', 'nickname', 'phone', 'login_password', 'other_info']);
        return (new UserLogic())->userGetTask();
    }

    //用户审核统计
    public function userCheck()
    {
        //ValidateUser::checkValidate($data, ['name', 'nickname', 'phone', 'login_password', 'other_info']);
        return (new UserLogic())->userCheck();
    }

    //根据电话状态返回客户
    public function userClientList($data)
    {
        return (new UserLogic())->userClientList($data);
    }

    //审核用户
    public function checkClientUser($data)
    {
        ValidateUser::checkValidate($data, ['card']);
        return (new UserLogic())->checkClientUser($data);
    }

    //用户联系时间
    public function userPhoneTime()
    {
        return (new UserLogic())->userPhoneTime();
    }

    //用户列表
    public function userList($data)
    {
        return (new UserLogic())->userList($data);
    }

}
